package com.neusoft.demo.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.demo.po.User;
import com.neusoft.demo.service.UserService;

@Controller
@RequestMapping("/user")
public class Usercontroller {

	private static final String Uname = null;
	@Autowired UserService userService;
	//跳转到添加用户
	@RequestMapping("toAddUser")
	public String toAddUser() {
		return "addUser";
	}
	//添加用户并重定向
	@RequestMapping("addUser")
	public String addUser(User user,Model model) {
		if (user!=null) {
			userService.saveUser(user);
		}
		return "redirect:/user/userInfo";
				
	}
	//修改用户
	@RequestMapping("updateUser")
	public String updateUser(User user,Model model) {
		if (userService.updateUser(user)) {
			user=userService.findUserById(user.getUid());
			model.addAttribute("user",user);
			return "redirect:/user/userInfo";
			
		}
		return "/error";
	}
	
	//查询所有用户
	@RequestMapping("/getAllUser")
	public String getAllUser(Model model) {
		List<User> user=userService.findAll();
		model.addAttribute("userList",user);
		return "allUser";
	}
	//查询单个用户
	@RequestMapping("/getUser")
	public String getUser(Integer uid,Model model) {
		model.addAttribute("user", userService.findUserById(uid));
		return "editUser";
	}
	
	//删除用户
	@RequestMapping("/delUser")
	public String deleteUser(Integer uid,Model model) {
		model.addAttribute("user",userService.deleteUser(uid));
		return "redirect:/user/userInfo";
		
	}
	//分页查询用户信息
	@RequestMapping("userInfo")
	public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		//从第I条开始，每页查询5条数据
		PageHelper.startPage(pn,5);
		List<User> users=userService.findAll();
		//将用户信息放入pageInfo对象中
		PageInfo page=new PageInfo(users,5);
		model.addAttribute("pageInfo",page);
	
		return "allUser";
	}
	//模糊查询
	@RequestMapping("findByName")
//	public String findByName(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
//		//从第I条开始，每页查询5条数据
//				PageHelper.startPage(pn,5);
//				List<User> users=userService.findByName(Uname);
//				//将用户信息放入pageInfo对象中
//				PageInfo page=new PageInfo(users,5);
//				model.addAttribute("pageInfo",page);
//			
//				return "allUser";
//	}
	public ModelAndView findByName(@RequestParam(value="pn",defaultValue="1")Integer pn,String uName,ModelAndView mv) {
		System.out.println(uName);
		PageHelper.startPage(pn,5);
		List<User> users = userService.findByName(uName);
		PageInfo page=new PageInfo(users,5);
		mv.addObject(page);
	

		mv.addObject("User",users);
		mv.setViewName("select");
		return mv;
	}
	
}
